package cn.exrick.xboot.modules.assessment.serviceimpl;

import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.assessment.dao.HkFormEntityDao;
import cn.exrick.xboot.modules.assessment.dao.HkFormRelationShipDao;
import cn.exrick.xboot.modules.assessment.entity.HkFormEntity;
import cn.exrick.xboot.modules.assessment.entity.HkFormRelationShip;
import cn.exrick.xboot.modules.assessment.service.HkFormEntityService;
import cn.exrick.xboot.modules.assessment.to.HkFormEntityTo;
import cn.exrick.xboot.modules.assessment.vo.HkFormEntityVo;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;


/**
 * 表单实体类接口实现
 * @author yck
 */
@Slf4j
@Service
@Transactional
public class HkFormEntityServiceImpl implements HkFormEntityService {

    @Autowired
    private HkFormEntityDao hkFormEntityDao;
    @Autowired
    private HkFormRelationShipDao hkFormRelationShipDao;
    @Override
    public HkFormEntityDao getRepository() {
        return hkFormEntityDao;
    }

    @Override
    public Page<HkFormEntity> findByCondition(HkFormEntity hkFormEntity, SearchVo searchVo, Pageable pageable) {

        return hkFormEntityDao.findAll(new Specification<HkFormEntity>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<HkFormEntity> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {

                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<Date> createTimeField = root.get("createTime");
                List<Predicate> list = new ArrayList<Predicate>();
                Path<String> formName = root.get("formName");

                if (StrUtil.isNotBlank(hkFormEntity.getFormName())){
                    list.add(cb.like(formName,'%'+hkFormEntity.getFormName()+'%'));
                }
                // 创建时间
                if(StrUtil.isNotBlank(searchVo.getStartDate())&&StrUtil.isNotBlank(searchVo.getEndDate())){
                    Date start = DateUtil.parse(searchVo.getStartDate());
                    Date end = DateUtil.parse(searchVo.getEndDate());
                    list.add(cb.between(createTimeField, start, DateUtil.endOfDay(end)));
                }


                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        }, pageable);
    }

    @Override
    @Transactional
    public void updateFormEntity(HkFormEntityVo formEntityVo) {

        hkFormRelationShipDao.deleteByFormId(formEntityVo.getId());
        List<HkFormRelationShip> formRelationShips = formEntityVo.getFormRelationShips();
        for (int i = 0; i < formRelationShips.size(); i++) {
            HkFormRelationShip ship = new HkFormRelationShip();
            ship.setFormId(formEntityVo.getId());
            ship.setItemId(formRelationShips.get(i).getItemId());
            ship.setModelName(formRelationShips.get(i).getModelName());
            ship.setSortNumber(i + 1);
            hkFormRelationShipDao.save(ship);
        }
        HkFormEntity hkFormEntity = hkFormEntityDao.findById(formEntityVo.getId()).orElse(null);
        hkFormEntity.setFlowId(formEntityVo.getFlowId());
        hkFormEntity.setFlowName(formEntityVo.getFlowName());
        hkFormEntity.setActTypeId(formEntityVo.getActTypeId());
        hkFormEntity.setActTypeName(formEntityVo.getActTypeName());
        hkFormEntity.setFormName(formEntityVo.getFormName());
        hkFormEntityDao.saveAndFlush(hkFormEntity);
    }

    @Override
    public HkFormEntity get(String s) {
        return hkFormEntityDao.findById(s).orElse(null);
    }

    @Override
    public List<HkFormEntity> findByFromName(String fromName) {
        return hkFormEntityDao.findByFormName(fromName);
    }
}